package com.sevenday.mybottomnagivationviewdemo.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.sevenday.mybottomnagivationviewdemo.R;
import com.sevenday.mybottomnagivationviewdemo.adapter.MyPageAdapter;
import com.sevenday.mybottomnagivationviewdemo.fragment.HomeFragment;
import com.sevenday.mybottomnagivationviewdemo.fragment.ImageFragment;
import com.sevenday.mybottomnagivationviewdemo.fragment.MessageFragment;
import com.sevenday.mybottomnagivationviewdemo.fragment.MineFragment;

import java.util.ArrayList;
import java.util.List;

public class BNVVPActivity extends AppCompatActivity {

    private static final String TAG = BNVVPActivity.class.getSimpleName();
    BottomNavigationView navigation;
    ViewPager vp;

    List<Fragment> fragmentList=new ArrayList<>();

    MyPageAdapter adapter;

    MenuItem prevMenuItem;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bnvvp);


        navigation=findViewById(R.id.bnv);
        vp=findViewById(R.id.vp);

        fragmentList.add(HomeFragment.newInstance());
        fragmentList.add(ImageFragment.newInstance());
        fragmentList.add(MessageFragment.newInstance());
        fragmentList.add(MineFragment.newInstance());

        adapter=new MyPageAdapter(getSupportFragmentManager(),fragmentList);

        vp.setAdapter(adapter);


        navigation.setOnNavigationItemSelectedListener(menuItem -> {

            switch (menuItem.getItemId()){
                case R.id.navigation_home:
                    vp.setCurrentItem(0);
                    return true;
                case R.id.navigation_image:
                    vp.setCurrentItem(1);
                    return true;

                case R.id.navigation_msg:
                    vp.setCurrentItem(2);
                    return true;

                case R.id.navigation_mine:
                    vp.setCurrentItem(3);
                    return true;

            }

            return false;
        });



        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.i(TAG, "onPageScrolled: "+position);
            }

            @Override
            public void onPageSelected(int position) {
                Log.i(TAG, "onPageSelected: "+position);
//                if (prevMenuItem != null) {
//                    prevMenuItem.setChecked(false);
//                } else {
//                    navigation.getMenu().getItem(0).setChecked(false);
//                }
//                navigation.getMenu().getItem(position).setChecked(true);
//                prevMenuItem = navigation.getMenu().getItem(position);

                navigation.setSelectedItemId(navigation.getMenu().getItem(position).getItemId());

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.i(TAG, "onPageScrollStateChanged: "+state);
            }
        });


        navigation.setSelectedItemId(navigation.getMenu().getItem(2).getItemId());

    }







}
